Imports Microsoft.VisualBasic
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports SubSonic
Imports ClubStarterKit.Data
Imports System.Data

Namespace ClubStarterKit.Web
    Public Class Blogs

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function GetAll(ByVal pagenum As Integer) As IDataReader
            Dim qry As New Query(Tables.BlogPosts)
            qry.OrderBy = OrderBy.Desc(BlogPosts.Columns.Postdate)
            qry.PageIndex = pagenum
            qry.QueryType = QueryType.Select
            qry.PageSize = ClubStarterKit.Web.Settings.PageSize()
            Return qry.ExecuteReader()
        End Function

        Public Shared Function RowCount() As Integer
            Dim qry As New Query(Tables.BlogPosts)
            Return qry.GetRecordCount()
        End Function

        Public Shared Function NumPages() As Integer
            Dim count As Integer = RowCount()
            Return Shared_Routines.NumPages(count)
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function GetPost(ByVal postid As String) As IDataReader
            Dim qry As New Query(Tables.BlogPosts)
            qry.AddWhere(BlogPosts.Columns.Postid, postid)
            qry.QueryType = QueryType.Select
            Dim rdr As SqlDataReader = qry.ExecuteReader()
            Return rdr
            rdr.Close()
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function GetPostComments(ByVal postid As String) As IDataReader
            Dim qry As New Query(Tables.BlogComments)
            qry.AddWhere(BlogComments.Columns.Postid, postid)
            qry.QueryType = QueryType.Select
            qry.OrderBy = OrderBy.Desc(BlogComments.Columns.Commentid)
            Dim rdr As SqlDataReader = qry.ExecuteReader()
            Return rdr
            rdr.Close()
        End Function

        <System.ComponentModel.DataObjectMethodAttribute _
        (System.ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function GetBloggerAll(ByVal pagenum As Integer, ByVal Username As String) As IDataReader
            Dim mem As MembershipUser = Membership.GetUser(Username)
            Dim memkey As String = mem.ProviderUserKey.ToString()
            Dim qry As New Query(Tables.BlogPosts)
            qry.AddWhere(BlogPosts.Columns.Memid, memkey)
            qry.OrderBy = OrderBy.Desc(BlogPosts.Columns.Postdate)
            qry.PageIndex = pagenum
            qry.QueryType = QueryType.Select
            qry.PageSize = ClubStarterKit.Web.Settings.PageSize()
            Return qry.ExecuteReader()
        End Function

        Public Shared Function RowCount(ByVal BloggerUsername As String) As Integer
            Dim mem As MembershipUser = Membership.GetUser(BloggerUsername)
            Dim memkey As String = mem.ProviderUserKey.ToString()
            Dim qry As New Query(Tables.BlogPosts)
            qry.AddWhere(BlogPosts.Columns.Memid, memkey)
            Return qry.GetRecordCount()
        End Function

        Public Shared Function NumPages(ByVal BloggerUsername As String) As Integer
            Dim count As Integer = RowCount(BloggerUsername)
            Return Shared_Routines.NumPages(count)
        End Function

        Public Shared Function NumPostsByMem(ByVal Memberid As Guid) As Integer
            Dim qry As New Query(Tables.BlogPosts)
            qry.AddWhere(BlogPosts.Columns.Memid, Memberid)
            qry.QueryType = QueryType.Select
            Return qry.GetRecordCount()
        End Function
    End Class
End Namespace